<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Post;
use App\Models\PostLike;
use App\Models\Notification;
use Illuminate\Http\Request;

class PostLikeController extends Controller
{
    /**
     * 点赞或取消点赞
     */
    public function toggle(Request $request, Post $post)
    {
        $user = $request->user();

        $like = PostLike::where('user_id', $user->id)
            ->where('post_id', $post->id)
            ->first();

        if ($like) {
            // 取消点赞
            $like->delete();

            return response()->json([
                'liked' => false,
                'likes_count' => $post->fresh()->likes_count,
                'message' => '取消点赞成功'
            ]);
        } else {
            // 点赞
            $like = PostLike::create([
                'user_id' => $user->id,
                'post_id' => $post->id,
            ]);

            // 创建通知（不通知自己）
            if ($post->user_id !== $user->id) {
                Notification::create([
                    'user_id' => $post->user_id,
                    'type' => 'like',
                    'notifiable_type' => PostLike::class,
                    'notifiable_id' => $like->id,
                    'from_user_id' => $user->id,
                    'title' => '新的点赞',
                    'content' => $user->name . ' 赞了你的文章《' . $post->title . '》',
                    'data' => [
                        'post_id' => $post->id,
                        'post_slug' => $post->slug,
                        'post_title' => $post->title,
                        'user_name' => $user->name,
                        'user_username' => $user->username,
                    ],
                ]);
            }

            return response()->json([
                'liked' => true,
                'likes_count' => $post->fresh()->likes_count,
                'message' => '点赞成功'
            ]);
        }
    }

    /**
     * 获取用户点赞的文章
     */
    public function userLikes(Request $request)
    {
        $user = $request->user();

        $likes = PostLike::with(['post' => function ($query) {
            $query->with(['user', 'category', 'tags']);
        }])
            ->where('user_id', $user->id)
            ->orderBy('created_at', 'desc')
            ->paginate(20);

        return response()->json([
            'likes' => $likes->items(),
            'pagination' => [
                'total' => $likes->total(),
                'current_page' => $likes->currentPage(),
                'per_page' => $likes->perPage(),
                'last_page' => $likes->lastPage(),
            ]
        ]);
    }

    /**
     * 检查用户是否点赞了文章
     */
    public function check(Request $request, Post $post)
    {
        $user = $request->user();

        if (!$user) {
            return response()->json(['liked' => false]);
        }

        $liked = PostLike::where('user_id', $user->id)
            ->where('post_id', $post->id)
            ->exists();

        return response()->json(['liked' => $liked]);
    }
}